package com.hmdp.service.impl;

import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.SeckillVoucher;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.service.ISeckillVoucherService;
import com.hmdp.service.IVoucherOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisIdWorker;
import com.hmdp.utils.UserHolder;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {

    @Resource
    private ISeckillVoucherService seckillVoucherService;
    @Resource
    private RedisIdWorker redisIdWorker;
    @Override
    public Result seckillVoucher(Long voucherId) {
        // 1.查询优惠券
        SeckillVoucher secKillVoucher = seckillVoucherService.getById(voucherId);
        // 2.1 判断秒杀是否开始
        if(secKillVoucher.getBeginTime().isAfter(LocalDateTime.now())){
            return Result.fail("秒杀尚未开始");
        }
        // 2.2 判断秒杀是否结束
        if(secKillVoucher.getEndTime().isBefore(LocalDateTime.now())){
            return Result.fail("秒杀已结束");
        }
        // 3.判断库存是否充足
        if(secKillVoucher.getStock() < 1){
            return Result.fail("库存不足");
        }
        Long UserId = UserHolder.getUser().getId();
        synchronized (UserId.toString().intern()){
            IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();
            return proxy.createVoucherOrder(voucherId);
        }
    }

    @Transactional
    public Result createVoucherOrder(Long voucherId) {
        // 判断当前用户是否已经买过了
        int count = query().eq("user_id", UserHolder.getUser().getId())
                .eq("voucher_id", voucherId)
                .count();
        if(count > 0){
            // 说明已经买过了
            return Result.fail("不能重复下单");
        }
        // 4.创建订单
        // 4.1 扣件库存
        boolean success = seckillVoucherService.update()
                .setSql("stock = stock - 1")
                .eq("voucher_id", voucherId)
                .gt("stock", 0)
                .update();
        if(!success){
            return Result.fail("库存不足");
        }
        // 4.2 获取用户ID
        Long id = UserHolder.getUser().getId();
        // 4.3 生成订单ID
        Long orderId = redisIdWorker.nextId("order");
        // 创建订单详情
        VoucherOrder voucherOrder = new VoucherOrder();
        // 订单ID
        voucherOrder.setId(orderId);
        // 用户ID
        voucherOrder.setUserId(id);
        // 优惠券ID
        voucherOrder.setVoucherId(voucherId);
        // 存储到数据库中
        save(voucherOrder);
        // 5.返回订单的ID
        return Result.ok(orderId);
    }
}
